//
//  TBGuideView.m
//  GuideViewDemo
//
//  Created by Apple on 2018/3/23.
//  Copyright © 2018年 techbird. All rights reserved.
//

#import "TBGuideView.h"
#import "Masonry.h"

@interface TBGuideView()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@end
@implementation TBGuideView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.frame = [UIScreen mainScreen].bounds;
    [self addSubview:scrollView];
    _scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
}

-(void)setImageNames:(NSArray *)imageNames{
    _imageNames = imageNames;
    for (NSInteger i = 0; i < imageNames.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imageNames[i] ofType:nil]];
        imageView.frame = CGRectMake(_scrollView.bounds.size.width * i, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height);
        [_scrollView addSubview:imageView];
        [self makeMoreButtonWithImageView:imageView];
        imageView.userInteractionEnabled = YES;
    }
    _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width * (imageNames.count + 1), 0);
}

- (void)makeMoreButtonWithImageView:(UIImageView *) imageView{
    UIButton *button = [[UIButton alloc]init];
    [button setImage:[UIImage imageNamed:@"common_more_black"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"common_more_white"] forState:UIControlStateHighlighted];
    [imageView addSubview:button];
    
    button.translatesAutoresizingMaskIntoConstraints = NO;
    //添加约束
    NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-20];
    [imageView addConstraint:trailing];
    NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeBottom multiplier:1 constant:-50];
    [imageView addConstraint:bottom];
    
    [button addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)moreBtnClick:(UIButton *) button{
    [UIView animateWithDuration:1.0 animations:^{
        button.superview.transform = CGAffineTransformMakeScale(2, 2);
        button.superview.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    if (page == _imageNames.count) {
        [self removeFromSuperview];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSInteger page =  scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
}


@end
